/*
 * @lc app=leetcode.cn id=69 lang=swift
 *
 * [69] x 的平方根
 */

// @lc code=start
class Solution {
    func mySqrt(_ x: Int) -> Int { //8
        var left = 0, right = x
        while left <= right {
            let middle = (left + right) / 2
            if middle * middle > x {
                right = middle - 1
            } else if middle * middle < x {
                left = middle + 1
            } else {
                return middle
            }
        }
        return left - 1
    }
}
// @lc code=end

